package com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth;

import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.shanxi.cz.framework.common.pojo.PageParam;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.pojo.CommonResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import static com.shanxi.cz.framework.common.pojo.CommonResult.success;

import com.shanxi.cz.framework.excel.core.util.ExcelUtils;

import com.shanxi.cz.framework.apilog.core.annotation.ApiAccessLog;
import static com.shanxi.cz.framework.apilog.core.enums.OperateTypeEnum.*;
import static com.shanxi.cz.module.biz.enums.LogRecordConstants.*;

import com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth.vo.*;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordmonth.AmmeterRecordMonthDO;
import com.shanxi.cz.module.biz.service.ammeterrecordmonth.AmmeterRecordMonthService;

@Tag(name = "管理后台 - 电表数据月报")
@RestController
@RequestMapping("/biz/ammeter-record-month")
@Validated
public class AmmeterRecordMonthController {

    @Resource
    private AmmeterRecordMonthService ammeterRecordMonthService;

    @PostMapping("/create")
    @Operation(summary = "创建电表数据月报")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:create')")
    public CommonResult<Integer> createAmmeterRecordMonth(@Valid @RequestBody AmmeterRecordMonthSaveReqVO createReqVO) {
        return success(ammeterRecordMonthService.createAmmeterRecordMonth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电表数据月报")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:update')")
    public CommonResult<Boolean> updateAmmeterRecordMonth(@Valid @RequestBody AmmeterRecordMonthSaveReqVO updateReqVO) {
        ammeterRecordMonthService.updateAmmeterRecordMonth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电表数据月报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:delete')")
    public CommonResult<Boolean> deleteAmmeterRecordMonth(@RequestParam("id") Integer id) {
        ammeterRecordMonthService.deleteAmmeterRecordMonth(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除电表数据月报")
                @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:delete')")
    public CommonResult<Boolean> deleteAmmeterRecordMonthList(@RequestParam("ids") List<Integer> ids) {
        ammeterRecordMonthService.deleteAmmeterRecordMonthListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电表数据月报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:query')")
    public CommonResult<AmmeterRecordMonthRespVO> getAmmeterRecordMonth(@RequestParam("id") Integer id) {
        AmmeterRecordMonthDO ammeterRecordMonth = ammeterRecordMonthService.getAmmeterRecordMonth(id);
        return success(BeanUtils.toBean(ammeterRecordMonth, AmmeterRecordMonthRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电表数据月报分页")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:query')")
    public CommonResult<PageResult<AmmeterRecordMonthRespVO>> getAmmeterRecordMonthPage(@Valid AmmeterRecordMonthPageReqVO pageReqVO) {
        PageResult<AmmeterRecordMonthDO> pageResult = ammeterRecordMonthService.getAmmeterRecordMonthPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AmmeterRecordMonthRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电表数据月报 Excel")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-month:export')")
    @ApiAccessLog(operateType = EXPORT)
    @LogRecord(type = SYSTEM_USER_TYPE, subType = EXPORT_MONTH_REPORT_TYPE, bizNo = "{{#pageReqVO.ammeterId}}",
            success = EXPORT_MONTH_REPORT_SUCCESS)
    public void exportAmmeterRecordMonthExcel(@Valid AmmeterRecordMonthPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AmmeterRecordMonthDO> list = ammeterRecordMonthService.getAmmeterRecordMonthPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电表数据月报.xls", "数据", AmmeterRecordMonthRespVO.class,
                        BeanUtils.toBean(list, AmmeterRecordMonthRespVO.class));
    }

}